package com.ruoyi.web.controller.system;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.exception.StatusException;
import com.ruoyi.system.service.ISysUserService;

import javax.annotation.Resource;

public class AdminController extends BaseController {

    @Resource
    protected ISysUserService userService;

    /**
     * 获取登录用户id
     */
    @Override
    public Long getUserId() {
        Long userId = getLoginUser().getUserId();
        SysUser sysUser = userService.getById(userId);
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        return userId;
    }


    protected SysUser getUser() {
        Long userId = getLoginUser().getUserId();
        SysUser sysUser = userService.getById(userId);
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        if(!StrUtil.equals(sysUser.getRole(),"admin")){
            throw  new BusiException("无权限");
        }
        return sysUser;
    }

    public Long getCommonUserId() {
        Long userId = getLoginUser().getUserId();
        SysUser sysUser = userService.getById(userId);
        if (StrUtil.equals("1", sysUser.getStatus())) {
            throw new StatusException("账号已禁用");
        }
        return userId;
    }



}
